//
//  TimeSelector.m
//  XRSquare
//
//  Created by UaenaMo on 2018/8/17.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "TimeSelector.h"

@interface TimeSelector ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * tableView;
    NSArray * dataArr;
}
@end

@implementation TimeSelector

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        tableView = [[UITableView alloc]initWithFrame:self.bounds];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableFooterView = [UIView new];
        [self addSubview:tableView];
    }
    return self;
}

-(void)requestData
{
    [NetRequestCenter archivetimelistResult:^(BOOL success, NSDictionary *resultDic) {
        if (success) {
            dataArr = [resultDic objectForKey:@"data"];
            [tableView reloadData];
        }
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArr count] + 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifer = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    if (indexPath.row == [dataArr count])
    {
        cell.textLabel.text = @"重置";
    }
    else
    {
        cell.textLabel.text = [dataArr objectAtIndex:indexPath.row];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == [dataArr count])
    {
        [self.delegate selectedTime:@""];
    }
    else
    {
        [self.delegate selectedTime:[dataArr objectAtIndex:indexPath.row]];
    }
    [self removeFromSuperview];
}

@end
